

--注册常规合成

local crads = {
    '春秋蝉',
    '顷刻炼化',
    '逆流河',
    '梦蝶蛊',
    '子母连心蛊',
    '幻梦蝶蛊',
    '铁甲金蚕蛊',
    '剑遁蛊',
    '万家灯火蛊',
    '阴阳合欢蛊',
    '智慧蛊',
    '悔蛊',
    '定仙游蛊',
    
}
local has = {
    '春秋蝉',
    '梦蝶蛊',
    '子母连心蛊',
    '幻梦蝶蛊',
    '铁甲金蚕蛊',
    '剑遁蛊',
    '万家灯火蛊',
    '阴阳合欢蛊',
    '智慧蛊',
    '坚持蛊',
    '悔蛊',
    '定仙游蛊',
}

local mt = ac.skill['春秋蝉']

--入口
function mt:on_card(hero,player)
    if hero[self.name] then
        return
    end
    hero[self.name] = true
    for _,name in ipairs(crads) do
        player:set_data(name,true)
    end

    player:event '卡池-添加卡组'(function(_,_,pool)
        for _,name in ipairs(crads) do
            pool[#pool+1] = name
        end
        if hero:get_data('蛊世界-计数',0)>=6 then
            pool[#pool+1] = '宿命蛊'
        end
        local list = {}
        for _,temp in ipairs(get_card_list(hero,'蛊世界')) do
            if table.find(has,temp.name) then
                pool[#pool+1] = '顷刻炼化'
                return
            end
        end
    end)
    hero:event '天赋-检测合成后'(function(trg)
        local haves = hero:get_data('拥有天赋表') or {}
        for _,name in ipairs(crads) do
            if not haves[name] then
                return
            end
        end
        trg:remove()
        hero:set_data('完成-计数',hero:get_data('完成-计数',0) + 1)
    end)
end

ac.game:event '英雄-抽卡完毕'(function(_,hero,temp)
    if table.find(has,temp.name) then
        --LoadItemSX(hero,temp.data.base_attr,-1)
        for _,ski in ipairs(temp.abis) do
            ski:gc_clear()
        end

        temp.timer_count = ac.wait(200*1000,function(t)
            t:remove()
            hide_card(temp)
            --LoadItemSX(hero,temp.data.base_attr,1)
            for _,ski in ipairs(temp.abis) do
                if ski.on_add then
                    ski:on_add()
                end
            end
        end)
        temp:gc(temp.timer_count)
    end
end)

local mt = ac.skill['宿命蛊']
local crads = {
    '打破宿命',
    '至尊仙胎蛊',
}
function mt:on_card(hero,player)
    player:send_msg('|cffffff00至尊仙胎蛊|r加入卡池')
    player:send_msg('|cffffff00打破宿命|r加入卡池')
    player:event '卡池-添加卡组'(function(_,_,pool)
        for _,name in ipairs(crads) do
            pool[#pool+1] = '至尊仙胎蛊'
            pool[#pool+1] = '炼天魔尊'
            if hero:get_data('蛊世界-计数',0)>=8 then
                pool[#pool+1] = '打破宿命'
            end
        end
    end)
end

local mt = ac.skill['打破宿命']

function mt:on_card(hero,player)
    local skill = hero:find_skill('宿命蛊')
    local Lock = player:get_data('无法获得卡') or {}
    if skill then
        skill:remove()
    end
    Lock[skill.name] = true
    player:send_msg('|cffff0000大爱仙尊|r加入卡池')
    player:event '卡池-添加卡组'(function(_,_,pool)
        pool[#pool+1] = '大爱仙尊'
    end)
end


--=====================================================================================

local mt = ac.skill['时光逆流']
mt.model1 = [[e1aee662f7ab476c83d6429e87438f01.mdl]]
function mt:on_add()
    local hero = self.owner
    self:gc(hero:add_buff(self.name){
        art = self.art,
        tip = self:get_tip(),
        keep = true,
        on_add = function(buff)
            buff:gc(hero:event '单位-拦截死亡'(function(_,data)
                data.life_rate = 100
                local open = math.random(100)<=self.data1
                if open then
                    hero:add_effect(self.model1):remove()
                end
                return open
            end))
        end,
    })
end

local mt = ac.skill['顷刻炼化*']
function mt:on_add()
    local hero = self.owner
    local player = hero.owner
    local Lock = player:get_data('无法获得卡') or {}
    local list = {}
    for _,temp in ipairs(get_card_list(hero,'蛊世界')) do
        if table.find(has,temp.name) then
            table.insert(list,temp)
        end
    end
    if #list>0 then
        local temp = table.random(list)
        if temp.timer_count then
            temp.timer_count:on_timer()
        end
    end
    ac.wait(0,function()
        LoadItemSX(hero,self.owner_skill.base_attr,1)
        hide_card(self.owner_skill)
        local haves = hero:get_data('拥有天赋表') or {}
        for _,name in ipairs(has) do
            if haves[name] then
                Lock[self.owner_skill.name] = nil
                return
            end
        end
    end)
end

local mt = ac.skill['逆流河*']
function mt:on_add()
    local hero = self.owner
    local player = hero.owner
    local count = 180
    local Lock = player:get_data('无法获得卡') or {}
    self.owner_skill.timer_count = ac.wait(count*1000,function(t)
        self.owner_skill:remove()
        Lock[self.owner_skill.name] = true
        hero:notify('英雄-获得天赋',hero,'坚持蛊')
    end)
    self:gc(self.owner_skill.timer_count)
    self:gc(ac.loop(1000,function(t)
        hero:add(self['属性1'],-hero:get('生命上限') * self.data1 / 100)
        count = count - 1
        if count<=0 then
            t:remove()
        end
    end))
end


local mt = ac.skill['白光剑飞']
mt.missile_art = [[56cd4d744644c676b0d8f7481dcaaca4.mdx]]
function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-受到攻击命中'(function(_,_,target,data)
        if self:is_cooling()==false then
            self:cooling()
            local group = ac.selector('unit'):range(hero,900):enemy(hero):get(8)
            local damage = hero:get('生命上限') * self.data1 / 100
            for _,unit in ipairs(group) do
                local mover = hero:launch_missile(unit,self.missile_art,1200,0,function()
                    hero:create_damage(unit,damage,self)
                end)
                mover.mover:set_scale(0.5)
            end
        end
    end))
end

local mt = ac.skill['万家灯火']
function mt:on_add()
    local hero = self.owner
    self:gc(hero:add_buff '万家灯火'{
        art = self.art,
        keep = true,
        data1 = self.data1,
    })
end

local buff = ac.buff['万家灯火']
buff.tip = [[每拥有一个队友护甲加成+'data1'%]]
function buff:on_add()
    local hero = self.owner
    local num = 0
    self:gc(ac.loop(1000,function()
        self.stack = ac.player.online_count
        local count = self.stack * self.data1
        hero:add('护甲加成',count - num)
        num = count
    end))
    self:gc(function()
        hero:add('护甲加成', - num)
    end)
end

local mt = ac.skill['庄周梦蝶']
function mt:on_add()
    local hero = self.owner
    local player = hero.owner
    local count = 0
    self:gc(ac.loop(200,function()
        local ex = math.floor((100 - hero:get('当前生命百分比'))/self.data1) * self.data2
        hero:add('生命恢复%',ex - count)
        count = ex
    end))
    self:gc(function()
        hero:add('生命恢复%', - count)
    end)
end


local mt = ac.skill['阴阳不息']
mt.cool = 40
function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-获得状态'(function(_,hero,buff)
        if buff.name=='晕眩' or buff.name=='诱捕' then
            if not self:is_cooling() then
                self:cooling()
                hero:add_buff '护盾'{
                    shield = hero:get('生命上限'),
                    time = 5,
                }
            end
        end
    end))
end


local mt = ac.skill['灵机一动']
function mt:on_add()
    local hero = self.owner
    self:gc(ac.loop(self.data1 * 1000,function()
        self:gc(hero:add_buff(self.name){
            art = self.art,
            tip = '经验获取提升',
            keep = true,
            time = self.data2,
            on_add = function(buff)
                buff:gc(hero:add('经验加成',self.data3))
            end
        })
    end))
end

local mt = ac.skill['懊悔']
function mt:on_add()
    local hero = self.owner
    self:gc(ac.loop(self.data1 * 1000,function()
        for u in ac.selector('unit')
            : range(hero,600)
            : enemy(hero)
            : loop()
        do
            u:add_buff '懊悔'{data1 = self.data2,time = self.data3}
        end
    end))
end

local buff = ac.buff['懊悔']
buff.data1 = 0
function buff:on_add()
    self:gc(self.owner:add_effect([[6c10a3376824c094388be227c9b85bf3.mdl]],'overhead'))
    self:gc(self.owner:add('护甲加成',-self.data1))
end

local mt = ac.skill['定仙游']
mt.model1 = [[Abilities\Spells\Orc\Purge\PurgeBuffTarget.mdl]]
function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-受到伤害'(function(_,_,target)
        if odd_run(self.data1) then
            local damage = hero:get(self['属性1']) * self.data3 / 100 + hero:get(self['属性2']) * self.data4 / 100
            target:add_effect(self.model1,'origin',1)
            target:add_buff '晕眩'{time = self.data2}
            hero:create_damage(target,damage,self)
        end
    end))
end

local mt = ac.skill['仙胎重塑']
mt.cool = 180
mt.model1 = [[d79b6e36b5388413f69854db3a90e320.mdl]]

function mt:on_add()
    local hero = self.owner
    local player = hero.owner
    self:gc(hero:event '单位-受到伤害'(function(_,_,target)
        if not self:is_cooling() and hero:get('当前生命百分比')<self.data1 then
            self:cooling()
            hero:add_effect(self.model1):remove()
            hero:add('生命',hero:get('生命上限') * self.data2 / 100)
        end
    end))
end



local mt = ac.skill['炼蛊炼天']
function mt:on_add()
    local hero = self.owner
    for _,temp in ipairs(get_card_list(hero,'蛊')) do
        if temp.timer_count then
            temp.timer_count:on_timer()
        end
        hide_card(temp)
    end
    self:gc(hero:event '天赋-检测合成后'(function(_,_,temp)
        for _,temp in ipairs(get_card_list(hero,'蛊')) do
            if temp.timer_count then
                temp.timer_count:on_timer()
            end
            hide_card(temp)
        end
    end))
end

local mt = ac.skill['炼化']
function mt:on_add()
    local hero = self.owner
    for _,temp in ipairs(get_card_list(hero,'蛊')) do
        if temp.timer_count then
            temp.timer_count:on_timer()
        end
        hide_card(temp)
        LoadItemSX(hero,temp.base_attr,self.data1/100)
    end
    self:gc(hero:event '天赋-检测合成后'(function(_,_,temp)
        if temp.data.card_class1['蛊'] then
            if temp.timer_count then
                temp.timer_count:on_timer()
            end
            hide_card(temp)
            LoadItemSX(hero,temp.base_attr,self.data1/100)
        end
    end))
end

local mt = ac.skill['众生同寿']
function mt:on_add()
    local hero = self.owner
    local num1 = 0
    local num2 = 0
    
    self:gc(hero:add_buff(self.name){
        art = self.art,
        tip = self:get_tip(),
        keep = true,
        pulse = 1,
        not_show_pulse = 1,
        on_pulse = function(buff)
            local cards = hero:get_data('吞噬天赋表') or {}
            local count = 0
            for _,temp in ipairs(cards) do
                if temp.data.card_class1['蛊'] then
                    count = count + 1
                end
            end
            buff.stack = count
            local count1 = count * self.data1
            local count2 = count * self.data2
            hero:add(self['属性1'],count1 - num1)
            hero:add('生命恢复%',count2 - num2)
            num1 = count1
            num2 = count2
        end,
    })
end

local mt = ac.skill['众生皆薪']
function mt:on_add()
    local hero = self.owner
    local num1 = 0
    local num2 = 0
    self:gc(hero:add_buff(self.name){
        art = self.art,
        tip = self:get_tip(),
        keep = true,
        pulse = 1,
        not_show_pulse = 1,
        on_pulse = function(buff)
            local count = ac.player.online_count - 1
            local count1 = count * self.data1
            local count2 = count * self.data2
            buff.stack = count
            hero:add('生命加成',count1 - num1)
            hero:add('伤害增幅',count2 - num2)
            num1 = count1
            num2 = count2
        end,
    })
end


local mt = ac.skill['薪火']
function mt:on_add()
    local hero = self.owner
    self:gc(hero:event '单位-受到攻击命中'(function(_,_,target,data)
        local damage = data.damage * self.data1 / 100
        hero:create_damage(target,damage,self)
    end))
end


local mt = ac.skill['炼天魔尊']

function mt:on_card(hero,player)
    local Lock = player:get_data('无法获得卡') or {}
    Lock['大爱仙尊'] = true
    Lock['打破宿命'] = true
    Lock['顷刻炼化'] = true
end


local mt = ac.skill['大爱仙尊']
function mt:on_card(hero,player)
    local Lock = player:get_data('无法获得卡') or {}
    Lock['炼天魔尊'] = true
    Lock['顷刻炼化'] = true
end


local mt = ac.skill['血道杀招']
mt.model1 = [[324713139ed84879509455f988f16fd4.mdl]]
function mt:on_add()
    local hero = self.owner
    local life = 0
    self:gc(hero:event '单位-受到伤害'(function(_,_,target,data)
        life = life + data.real_damage
        if life>hero:get('生命上限')*self.data1/100 then
            life = 0
            local point = target:get_point()
            local damage = hero:get(self['属性1']) * self.data2 / 100 + hero:get(self['属性2']) * self.data3 / 100
            local hx = (hero:get('生命上限') - hero:get('生命')) * self.data4/100
            hero:add('生命',hx)
            hero:aoe_damage(point,600,damage,self,function(u)
                u:add_effect(self.model1):remove()
            end)
        end
    end))
end